package com.atguigu.gmall.controller;

import com.atguigu.gmall.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.service.TradeStatsService;
import com.atguigu.gmall.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2024/4/29
 * 交易域统计Controller
 * @Controller 将类对象的创建以及关系的维护交给Spring，如果方法的返回值是String，会进行页面的跳转
 * 如果不想进行页面的跳转，可以在方法上加一个注解 @ResponseBody
 * @RestController 将类对象的创建以及关系的维护交给Spring，如果方法的返回值是String，不会进行页面的跳转，会将字符串直接返回给客户端
 * @RestController =  @Controller + @ResponseBody
 * http://localhost:8070/gmv
 * 地址中携带的参数到方法中可以自动类型转换
 */
@Controller
@RestController
public class TradeStatsController {

    //声明接口而不是实现类，保证低耦合
    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        //如果请求的时候没有传递日期参数，那么将当前日期赋值给date
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": " + gmv + "\n" +
                "}";
        return json;
    }


    /*
    @RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        //调用service方法，获取某天各个省份交易额
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);

        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");

        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceOrderAmount = provinceAmountList.get(i);
            jsonB.append("{\"name\": \"" + provinceOrderAmount.getProvinceName() + "\",\"value\": " + provinceOrderAmount.getOrderAmount() + "}");
            if (i < provinceAmountList.size() - 1) {
                jsonB.append(",");
            }
        }

        jsonB.append("],\"valueName\": \"交易额\"}}");

        return jsonB.toString();
    }
    */
    //MVC会将返回的对象封装为json字符串、将json转换为Map、将json数组封装为List
    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        //调用service方法，获取某天各个省份交易额
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);

        Map resMap = new HashMap<>();

        resMap.put("status", 0);
        
        HashMap dataMap = new HashMap();
        List dataList = new ArrayList();
        
        for (TradeProvinceOrderAmount provinceOrderAmount : provinceAmountList) {
            Map map = new HashMap();
            map.put("name", provinceOrderAmount.getProvinceName());
            map.put("value", provinceOrderAmount.getOrderAmount());
            dataList.add(map);
        }

        dataMap.put("mapData", dataList);
        dataMap.put("valueName", "交易额");
        resMap.put("data",dataMap);

        return resMap;
    }
    
}
